//
//  HJComprehensiveRankController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJComprehensiveRankController.h"
#import "HJCompresenCell.h"

@interface HJComprehensiveRankController ()

@property(nonatomic,strong) NSArray * titleArr;
@property(nonatomic,assign) NSIndexPath * cellRow;
@property(nonatomic,assign) NSInteger sort;
@property(nonatomic,strong) NSString * btnTitle;
@end

@implementation HJComprehensiveRankController
-(NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"综合排序",@"发布时间由先到后",@"发布时间由后到先",@"价格由低到高",@"价格由高到低",@"涨幅由低到高",@"涨幅由高到低"];
    }
    return _titleArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:HJColorRGB(255)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:HJColorRGB(255)]];
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftNavImageName = @"close";
    NSNumber * row = [[NSUserDefaults standardUserDefaults] objectForKey:@"row"];
    self.cellRow = [NSIndexPath indexPathForRow:[row integerValue] ? [row integerValue] : 0 inSection:0];
    self.btnTitle = @"综合排序";
}

-(void)didClickLeftNavAction {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    [self.tableView registerCellByNibName:@"HJCompresenCell"];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCompresenCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HJCompresenCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.title.text = self.titleArr[indexPath.row];
    
    if (indexPath.row == self.cellRow.row) {
        cell.imageHide = YES;
    }
    if (!cell.imageHide) {
        cell.selectImage.hidden = YES;
    }else {
        cell.selectImage.hidden = NO;
    }
    [self createLine:cell];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(void)createLine:(UITableViewCell *)cell {
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 79, mScreenWidth-30, 1)];
    line.backgroundColor= HJColorRGB(0);
    line.alpha = 0.05;
    [cell.contentView addSubview:line];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCompresenCell * cell =  (HJCompresenCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell.imageHide) {
        return;
    }else {
        cell.imageHide = YES;
        cell.selectImage.hidden = NO;
        self.btnTitle = self.titleArr[indexPath.row];
        self.sort = indexPath.row;
        [[NSUserDefaults standardUserDefaults] setObject:@(indexPath.row) forKey:@"row"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (self.cellRow) {
            HJCompresenCell * cell1 =  (HJCompresenCell *)[tableView cellForRowAtIndexPath:self.cellRow];
            cell1.imageHide = NO;
            cell1.selectImage.hidden = YES;
        }
    }
    self.cellRow = indexPath;
    
    
    if (self.bock) {
        self.bock(self.sort,self.btnTitle);
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    });
}
@end
